package com.oig.sys.workflow.msg.impl;

import cn.hutool.core.util.StrUtil;
import com.oig.common.util.JsonUtil;
import com.oig.sys.msg.api.TaskService;
import com.oig.sys.msg.dto.TaskDto;
import com.oig.sys.workflow.dto.AuditCallMsgDto;
import com.oig.sys.workflow.msg.IDepTaskService;
import com.oig.sys.workflow.vo.AutoOperationVo;
import com.oig.sys.workflow.vo.AutoRuleVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@RequiredArgsConstructor
@Slf4j
public class DepTaskServiceImpl implements IDepTaskService {

    private final TaskService taskService;

    @Override
    public void addAutoRuleTask(AutoRuleVo autoRuleVo, AutoOperationVo autoOperation, AuditCallMsgDto auditCallMsgDto) {
        TaskDto taskDto = new TaskDto();
        taskDto.setTenantId(autoRuleVo.getTenantId());
        taskDto.setTenantName(autoRuleVo.getTenantName());
        taskDto.setRelationType(autoRuleVo.getRelationType());
        Map<String, Object> objectMap = JsonUtil.stringToCollect(auditCallMsgDto.getObjJson());
        taskDto.setRelationId(String.valueOf(objectMap.get(autoRuleVo.getRelationIdColumn())));
        taskDto.setTaskTitle(autoOperation.getTaskTitle());
        taskDto.setTaskContent(StrUtil.format(autoOperation.getTaskContent(), objectMap));
        taskDto.setTaskPriority(autoOperation.getTaskPriority());
        taskDto.setTaskLevel(autoOperation.getTaskLevel());
        taskDto.setDealId(autoOperation.getTaskUserId());
        taskDto.setDealName(autoOperation.getTaskUserName());
        taskDto.setWatchId(autoOperation.getTaskWatchId());
        taskDto.setWatchName(autoOperation.getTaskWatchName());
        log.debug("add task:{}",taskDto);
        taskService.addTask(taskDto);
    }
}
